# rapid_funs.py

r'''Rapid functions that can be used in :class:`.cutting_pass.gen_pass`.
'''

from ot_simulator.transform import transform
from ot_simulator.template import param_funs

def null_offset(point, params, dest_machine):
    r'''No offset is applied, just returns point.
    '''
    return point

class rapid1(object):
    def __init__(self, axis, offset):
        self.axis = axis.lower()
        self.offset = offset

    def __call__(self, point, params, dest_machine):
        moved_point = point.move(**{self.axis:
                                    param_funs.get_value(self.offset, params)})
        dest_machine.rapid(moved_point, params)

class rapid2(object):
    def __init__(self, axis, offset):
        self.axis = axis.lower()
        self.offset = offset

    def __call__(self, point, params, dest_machine):
        moved_point = point.move(**{self.axis:
                                    param_funs.get_value(self.offset, params)})
        offset_point = dest_machine.with_offset(moved_point, params)
        positions = dict(zip('XYZ', offset_point.local_to_global()))
        del positions[self.axis.upper()]
        dest_machine.rapid_to((' '.join("{0}{1:.4f}".format(a, positions[a])
                                          for a in sorted(positions.keys()))),
                              params)
        dest_machine.rapid(moved_point, params)

class rapid2_work_coord(object):
    def __init__(self, axis, offset):
        self.axis = axis.lower()
        self.offset = offset

    def __call__(self, point, params, dest_machine):
        pre_transform = \
          transform.translation(**{self.axis: 
                                   -param_funs.get_value(self.offset, params)})
        point = pre_transform.inverse().add(point)
        offset_point = dest_machine.with_offset(point, params)
        positions = dict(zip('XYZ', offset_point.local_to_global()))
        del positions[self.axis.upper()]
        dest_machine.rapid_to((' '.join("{0}{1:.4f}".format(a, positions[a])
                                          for a in sorted(positions.keys()))),
                              params)
        dest_machine.rapid(point, params)
